package com.hefei.api.customer.manager.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hefei.api.customer.client.CustomerUrlConstants;
import com.hefei.api.customer.manager.ICustomerManager;
import com.hefei.api.customer.vo.Contact;
import com.hefei.api.customer.vo.Customer;
import com.hefei.api.customer.vo.SysMenu;
import com.hefei.common.exception.ClientException;
import com.hefei.common.http.client.ClientFactory;
import com.hefei.common.http.request.RequestHead;
import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.http.util.SendRequest;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;

public class CustomerManager implements ICustomerManager {
	private Logger logger = Logger.getLogger(CustomerManager.class);

	@Override
	public String saveCustomerInfo(Customer customer)
			throws ClientException {

		long timer = System.currentTimeMillis();
		logger.info("saveCustomerInfo time:" + timer);
		RequestHead head = null;
		String returnCode = null;

		try {
			head = RequestHead.get(timer, ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("customer", customer);
			String result = SendRequest.send(
					CustomerUrlConstants.CUSTOMER_SERVER_ADD_CUSTOMER_URL,
					dataJson, timer, head);
			logger.info("addcustomer time:" + timer + " result:" + result
					+ " cost:" + (System.currentTimeMillis() - timer));

			BaseResponse baseResponse = JacksonUtil.jsonToBean(result,
					BaseResponse.class);

			returnCode = baseResponse.getReturnCode();

		} catch (Exception e) {
			throw new ClientException(((ClientException) e).getErrorCode());
		}
		return returnCode;
	}

	@Override
	public String editCustomerInfo(Customer customer)
			throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("saveCustomerInfo time:" + timer);
		RequestHead head = null;
		String returnCode = null;
		try {
			head = RequestHead.get(timer, ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("customer", customer);
			String result = SendRequest.send(CustomerUrlConstants.CUSTOMER_SERVER_EDIT_CUSTOMER_URL, dataJson, timer, head);
			logger.info("editCustomerInfo time:" + timer + " result:" + result
					+ " cost:" + (System.currentTimeMillis() - timer));
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result,
					BaseResponse.class);
			
		} catch (Exception e) {
			throw new ClientException(((ClientException) e).getErrorCode());
		}
		return returnCode;
	}

	@Override
	public String deleteCustomerInfo(int customerId) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("saveCustomerInfo time:" + timer);
		RequestHead head = null;
		String returnCode = null;
		try {
			head = RequestHead.get(timer, ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("id",customerId);
			String result = SendRequest.send(CustomerUrlConstants.CUSTOMER_SERVER_DELETE_CUSTOMER_URL, dataJson, timer, head);
			logger.info("editCustomerInfo time:" + timer + " result:" + result
					+ " cost:" + (System.currentTimeMillis() - timer));
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result,
					BaseResponse.class);
			
			
		} catch (Exception e) {
			throw new ClientException(((ClientException) e).getErrorCode());
		}
		return returnCode;
	}

	@Override
	public Customer getCustomerById(int id) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("id", id);
			String result = SendRequest.send(CustomerUrlConstants.CUSTOMER_SERVER_GET_CUSTOMER_BY_ID, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String ObjStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				Customer customer = JacksonUtil.jsonToCollections(ObjStr, Customer.class);
				return customer;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public Customer getCustoemrByName(String name) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("name", name);
			String result = SendRequest.send(CustomerUrlConstants.CUSTOMER_SERVER_GET_CUSTOMER_BY_NAME, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String ObjStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				Customer customer = JacksonUtil.jsonToCollections(ObjStr, Customer.class);
				return customer;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<Contact> getContactById(int customerId) throws ClientException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long getDatagridTotal(Customer customer) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("login time:" + timer );
		RequestHead head = null;
		Long total = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("customer", customer);
			String result = SendRequest.send(CustomerUrlConstants.SYSMENU_GET_DATAGRID_TOTAL, dataJson, timer, head);
			logger.info("login time:" + timer + " result:" + result + " cost:" + (System.currentTimeMillis() - timer));
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String jsonStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				Map<String, Object> map = JacksonUtil.convertToMaps(jsonStr);
				total = (Long) map.get("total");
			}
		} catch (Exception e) {
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		return total;
	}

}
